﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class MobileInfo
    {
        private readonly KeRui.DAL.MobileInfo dal = new KeRui.DAL.MobileInfo();

        public int Add(KeRui.Model.MobileInfo model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.MobileInfo> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.MobileInfo> list = new List<KeRui.Model.MobileInfo>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.MobileInfo item = new KeRui.Model.MobileInfo();
                    if (dt.Rows[i]["ID"].ToString() != "")
                    {
                        item.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                    }
                    item.Version = dt.Rows[i]["Version"].ToString();
                    item.Name = dt.Rows[i]["Name"].ToString();
                    if (dt.Rows[i]["Type"].ToString() != "")
                    {
                        item.Type = int.Parse(dt.Rows[i]["Type"].ToString());
                    }
                    if (dt.Rows[i]["Way"].ToString() != "")
                    {
                        item.Way = int.Parse(dt.Rows[i]["Way"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int ID)
        {
            this.dal.Delete(ID);
        }

        public bool Exists(int ID)
        {
            return this.dal.Exists(ID);
        }

        public List<KeRui.Model.MobileInfo> GetList(string strWhere)
        {
            string strOrder = "Type DESC";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("MobileInfo", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.MobileInfo> GetListByCache(string strCondition)
        {
            string cacheKey = "MobileInfoList-" + strCondition;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(strCondition);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.MobileInfo>) cache;
        }

        public KeRui.Model.MobileInfo GetModel(int ID)
        {
            return this.dal.GetModel(ID);
        }

        public KeRui.Model.MobileInfo GetModelByCache(int ID)
        {
            string cacheKey = "MobileInfoModel-" + ID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.MobileInfo) cache;
        }

        public void Update(KeRui.Model.MobileInfo model)
        {
            this.dal.Update(model);
        }
    }
}

